package com.tuniu.activity;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.banner.BannerHomeFragment;
import com.tuniu.R;
import com.tuniu.data.detailgentuan.Data;
import com.tuniu.data.detailgentuan.Detailgentuan;
import com.tuniu.data.detailgentuan.Image;
import com.tuniu.data.detailheard2.CouponList;
import com.tuniu.data.detailheard2.Detailheard2;
import com.tuniu.data.detailheard2.Period;
import com.tuniu.utils.MyAPIUtils;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.ResponseListener;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DetailGenTuanActivity extends FragmentActivity {

	private MyPagerAdapter myPagerAdapter;

	private ArrayList<Image> imageList = new ArrayList<Image>();

	private TextView tv_price;

	private TextView tv_promotionDes;

	private TextView tv_shoufu;

	private TextView tv_youhui1;

	private TextView tv_youhui2;

	private TextView tv_youhui3;

	private TextView youhui_num;

	private TextView tv_lingjuan1;

	private TextView tv_lingjuan2;

	private TextView tv_lingjuan3;

	private TextView tv_lingjuan4;

	private TextView lingjuan_num;

	private TextView departCity;

	private ListView fenqiListView;

	private MyAdapter myAdapter;
	private ArrayList<Period> periodList =new ArrayList<Period>();

	private RelativeLayout fenqi;

	private LayoutInflater layoutInflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_gen_tuan);

		initUI();
		initData();
	}

	private void initData() {

		Intent intent = getIntent();
		String productId = intent.getStringExtra("productId");
		MyAPIUtils.getDetailGentuan(this, productId, new ResponseListener() {

			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(DetailGenTuanActivity.this, "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				Detailgentuan detailgentuan = GsonUtils.parseJSON(arg0, Detailgentuan.class);
				Data data = detailgentuan.getData();
				List<Image> images = data.getImages();
				imageList.clear();
				imageList.addAll(images);
				myPagerAdapter.notifyDataSetChanged();
				TextView name = (TextView) findViewById(R.id.detail_name);
				TextView tv_type = (TextView) findViewById(R.id.tv_type);
				TextView tv_typeNum = (TextView) findViewById(R.id.tv_typeNum);
					name.setText(data.getName());
				tv_type.setText(data.getCategory());
				tv_typeNum.setText("产品编号 : " + data.getProductId());


			}

			@Override
			public void onErrorResponse(VolleyError arg0) {

			}
		});
		
		MyAPIUtils.getDetail2(this, productId, new ResponseListener() {
			
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(DetailGenTuanActivity.this, "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}	
				Detailheard2 detailheard2 = GsonUtils.parseJSON(arg0, Detailheard2.class);
				com.tuniu.data.detailheard2.Data data = detailheard2.getData();
				int lowestPromoPrice = data.getLowestPromoPrice();
				int lowestPrice = data.getLowestPrice();
				youhui_num.setText(data.getPromotionList().size()+"个");
				tv_price.setText(lowestPromoPrice+"");
				tv_promotionDes.setText("已省"+(lowestPrice-lowestPromoPrice)+"元");
				tv_shoufu.setText(data.getDefaultStaging());
				List<String> promotionNameList = data.getPromotionNameList();
				for (int i = 0; i < promotionNameList.size(); i++) {
					if(i==0){
						tv_youhui1.setText(promotionNameList.get(i));
						tv_youhui1.setVisibility(View.VISIBLE);
					}else if(i==1){
						tv_youhui2.setText(promotionNameList.get(i));
						tv_youhui2.setVisibility(View.VISIBLE);
					}else if(i==3){
						tv_youhui3.setText(promotionNameList.get(i));
						tv_youhui3.setVisibility(View.VISIBLE);
					}
				}
				List<CouponList> couponList = data.getCouponList();
				for (int i = 0; i < couponList.size(); i++) {
					if(i==0){
						tv_lingjuan1.setText(couponList.get(i).getActivityTitle());
						tv_lingjuan1.setVisibility(View.VISIBLE);
					}else if(i==1){
						tv_lingjuan2.setText(couponList.get(i).getActivityTitle());
						tv_lingjuan2.setVisibility(View.VISIBLE);
					}else if(i==2){
						tv_lingjuan3.setText(couponList.get(i).getActivityTitle());
						tv_lingjuan3.setVisibility(View.VISIBLE);
					}else if(i==3){
						tv_lingjuan4.setText(couponList.get(i).getActivityTitle());
						tv_lingjuan4.setVisibility(View.VISIBLE);
					}
				}
				lingjuan_num.setText(couponList.size()+"个");
				departCity.setText("出发城市:"+data.getDepartCity().getDepartCityName());
				
				List<Period> periods = data.getPeriods();
				periodList.clear();
				periodList.addAll(periods);
				
				List<String> features = data.getFeatures();
				View tese = layoutInflater.inflate(R.layout.item_tese, null);
				TextView tv_features = (TextView) tese.findViewById(R.id.features);
				for (int i = 0; i <features.size(); i++) {
					tv_features.setText(features.get(i)+"\n");
				}
				fenqiListView.addFooterView(tese);
				myAdapter.notifyDataSetChanged();
				
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
		
		
		
		
		
		
		

	}

	private void initUI() {
		layoutInflater = getLayoutInflater();
		tv_price = (TextView) findViewById(R.id.tv_promotionPrice);
		tv_promotionDes = (TextView) findViewById(R.id.tv_promotionDes);
		tv_shoufu = (TextView) findViewById(R.id.tv_shoufu);
		tv_youhui1 = (TextView) findViewById(R.id.tv_youhui1);
		tv_youhui2 = (TextView) findViewById(R.id.tv_youhui2);
		tv_youhui3 = (TextView) findViewById(R.id.tv_youhui3);
		youhui_num = (TextView) findViewById(R.id.youhui_num);
		tv_lingjuan1 = (TextView) findViewById(R.id.tv_lingjuan1);
		tv_lingjuan2 = (TextView) findViewById(R.id.tv_lingjuan2);
		tv_lingjuan3 = (TextView) findViewById(R.id.tv_lingjuan3);
		tv_lingjuan4 = (TextView) findViewById(R.id.tv_lingjuan4);
		lingjuan_num = (TextView) findViewById(R.id.lingjuan_num);
		departCity = (TextView) findViewById(R.id.departCity);
		fenqi = (RelativeLayout) findViewById(R.id.relative_fenqi);
		findViewById(R.id.shoufu).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				fenqi.setVisibility(View.VISIBLE);
			}
		});
		ViewPager mPager = (ViewPager) findViewById(R.id.detail_viewPager);
		myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
		mPager.setAdapter(myPagerAdapter);
		
		fenqiListView = (ListView) findViewById(R.id.listView_fenqi);
		myAdapter = new MyAdapter();
		View layout_feiqi = layoutInflater.inflate(R.layout.fenqi_headview, null);
		fenqiListView.addHeaderView(layout_feiqi);
		
		
		
		fenqiListView.setAdapter(myAdapter);

	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return periodList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View layout = layoutInflater.inflate(R.layout.item_fenqi, null);
			TextView stagingTimes = (TextView) layout.findViewById(R.id.stagingTimes);
			TextView stagingPayDescription = (TextView) layout.findViewById(R.id.stagingPayDescription);
			TextView stagingHandingCharge = (TextView) layout.findViewById(R.id.stagingHandingCharge);
			for (int i = 0; i < periodList.size(); i++) {
				Period period = periodList.get(position);
				stagingTimes.setText(period.getStagingTimes());
				stagingPayDescription.setText(period.getStagingPayDescription());
				stagingHandingCharge.setText(period.getStagingHandingCharge());
			}
			
			return layout;
		}
		
	}
	
	class MyPagerAdapter extends FragmentStatePagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			String imagUrl = imageList.get(position).getBimage();
			return new BannerHomeFragment(imagUrl);
		}

		@Override
		public int getCount() {
			return imageList.size();
		}

	}

}
